package matrix;

import java.util.HashSet;
import java.util.Set;

/*
 *@description 有效的数独
 *@author hkfan
 *@version 1.0
 *@create 2025/6/3 17:08
 */
public class LeetCode_36 {
    public boolean isValidSudoku(char[][] board) {

        //1. 逐行判断
        for (int i = 0; i < board.length; i++) {
            Set<Character> set = new HashSet();
            for (int j = 0; j < board[0].length; j++) {
                if (set.contains(board[i][j])){
                    return false;
                }
                if (board[i][j]>='0' && board[i][j]<='9'){
                    set.add(board[i][j]);
                }
            }
        }

        //2. 逐列判断
        for (int j = 0; j < board[0].length; j++) {
            Set<Character> set = new HashSet();
            for (int i = 0; i < board.length; i++) {
                if (set.contains(board[i][j])){
                    return false;
                }
                if (board[i][j]>='0' && board[i][j]<='9'){
                    set.add(board[i][j]);
                }
            }
        }

        //3. 子区域判断
        for (int m = 0; m < 3; m++) {  //3行
            for (int n = 0; n < 3 ; n++) {  //3列
                Set<Character> set = new HashSet();
                for (int i = m*3; i <= m*3+2 ; i++) {
                    for (int j = n*3; j <= n*3+2; j++) {
                        if (set.contains(board[i][j])){
                            return false;
                        }
                        if (board[i][j]>='0' && board[i][j]<='9'){
                            set.add(board[i][j]);
                        }
                    }
                }

            }

        }

        return true;




    }
}
